<?php

namespace Common\Model;
use Think\Model;

class AreaGoodsModel extends Model{

    protected $_validate = array(
        array( 'area_id', 'require', '地区名称必须填写' ),
        array( 'goods_id', 'require', '商品名称必须填写' ),
        array( 'start_sell_time', 'require', '开始出售时间必须填写' ),
        array( 'end_sell_time', 'require', '截止出售时间必须填写' ),
        array( 'start_deliver_time', 'require', '开始配送时间必须填写' ),
        array( 'end_deliver_time', 'require', '截止配送时间必须填写' ),
        array( 'goods_storage', 'require', '库存数量必须填写' ),
    );
    
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 新增或更新
     * 
     * @return boolean fasle 失败 ， int 成功 返回完整的数据
     */
    public function update($data)
    {
        $data = $this->create($data);
        if (empty($data)) {
            return false;
        }
        if (empty($data['id'])) {
            $id = $this->add();
            if (! $id) {
                $this->error = '新增出错';
                return false;
            }
        } else {
            $result = $this->where(array( 'id' => $data['id'] ))->save($data);
            if (false === $result) {
                $this->error = '更新出错';
                return false;
            }
        }
        
        return $data;
    }


    public function getMemberOrderGoods($memberID, $menuID)
    {
        if (empty($memberID) || empty($menuID)){
            return false;
        }

        $join = 'INNER JOIN __ORDER__ AS o ON g.order_id = o.order_id';
        $field = 'o.order_id,o.order_sn,o.member_id,o.create_time,o.pay_state,o.pay_id,o.pay_amount,m.openid,m.nickname';

        $where= array();
        $where['g.menu_id'] = $menuID;
        $where['g.member_id'] = $memberID;
        $where['o.pay_state'] = 1;
        $result = $this->table('__ORDER_GOODS__ AS g')->join($join)->field($field)->where($where)->select();
        return $result;

    }

}
